import React, { forwardRef, useImperativeHandle, useState } from 'react';

import { Button, DatePicker, Form, Input, Modal, Radio, Tag, Select, InputNumber, message, Row, Col } from 'antd';
import { modalLayoutLg, modalLayoutSm } from '@/config/layout';
import { request } from 'umi';
import moment from 'moment';
const { TextArea } = Input;
const AppVersionModal = (props: any, refs: any) => {
  const [form] = Form.useForm();
  // const [fileList,setfileList] = useState([]);
  const [measureClassData, setmeasureClassData] = useState([]);
  const { setFieldsValue, resetFields } = form;
  const [code, setCode] = useState('');
  const [isModalVisible, setIsModalVisible] = useState(false);

  //显示弹窗
  const showModal = () => {
    setIsModalVisible(true);
  };

  //取消
  const handleCancel = () => {
    setIsModalVisible(false);
    setCode('');
    resetFields();
    props.callback && props.callback();
  };
  //确定
  const handleOk = () => {
    form
      .validateFields()
      .then((vals) => {
        let Obj = form.getFieldsValue();
        Obj = {
          ...Obj,

        };

        const url = code ? "/sys/app-version/edit" : "/sys/app-version/add";
        if (code) {
          Obj.id = code;
        } else {
        }
        if (Obj.date) {
          Obj['date'] = moment(Obj['date']).format('YYYY-MM-DD HH:mm:ss');
        }
        request(url, {
          method: 'POST',
          headers: {
            'Content-Type': 'application/json',
          },
          data: Obj
        }).then((res) => {
          message.success(res?.meta?.msg);
          setIsModalVisible(false);
          handleCancel();
        })
      })
      .catch((info) => { });
  };

  const getDetail = (id: any) => {
    request("/sys/app-version/queryById?id=" + id, {
      method: 'GET',
      headers: {
        'Content-Type': 'application/json',
      },
      data: {
        id
      }
    }).then((res) => {
      let obj = res?.data;
      if (obj['date']) {
        obj['date'] = moment(obj['date']);
      }
      setFieldsValue(obj);
      showModal();
    })


  };
  useImperativeHandle(refs, () => ({
    init: (id: any, record: any) => {
      if (id) {
        setCode(id);
        getDetail(id)

      } else {
        resetFields();
        showModal();
      }

      // setfileList([]);
    },
  }));

  return (
    <>
      <Modal
        title={code === '' ? '新增' : '编辑'}
        visible={isModalVisible}
        onOk={handleOk}
        onCancel={handleCancel}
        centered
        width={1000}
        maskClosable={false}
        bodyStyle={{
          maxHeight: window.innerHeight - 150,
          overflowY: 'auto',
        }}
      >
        <Form form={form} layout="vertical" {...modalLayoutSm}>
          <Row>
            <Col span={12}>
              <Form.Item label="版本号" name="versionCode"
                rules={[{ required: true, message: '请输入版本号!' }]}
              >
                <Input />
              </Form.Item>
            </Col>
            <Col span={12}>
              <Form.Item label="下载地址" name="address"
                rules={[{ required: true, message: '请输入下载地址!' }]}
              >
                <Input />
              </Form.Item>
            </Col>
            <Col span={12}>
              <Form.Item label="版本日期" name="date">
                <DatePicker showTime style={{ width: '100%' }} />
              </Form.Item>
            </Col>
            <Col span={12}>
              <Form.Item label="更新说明" name="description"
                rules={[{ required: true, message: '请输入更新说明!' }]}
              >
                <TextArea rows={4} />
              </Form.Item>
            </Col>

          </Row>
        </Form>
      </Modal>
    </>
  );
};
const AddMCom = forwardRef(AppVersionModal);

export default AddMCom;
